<template>
  <div class="myContainer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{this.title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="add">新 增</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">     
    	  <el-col :span="5">
        <template>
          <el-cascader
          placeholder="全部项目/小区"
          :options="regionOptions"
          filterable
          change-on-select
          v-model="regionOptSelected"
          ></el-cascader>
        </template>
      </el-col>
      <el-col :span="5">
        <el-input placeholder="请输入关键词" v-model="kw"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="fuzzySearch">查 询</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px">
      <el-col :span="24">
        <el-table
          ref="multipleTable"
          :data="list"
          :border = true
          :stripe = true
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            prop=""
             label="序号"
            width="55">
          </el-table-column>
          <el-table-column
            prop="epc_Id"
            label="物理卡号"
            >
          </el-table-column>
          <el-table-column
            prop="card_no"
            label="逻辑卡号"
            >             
          </el-table-column>         
          <el-table-column
            prop="card_type"
            label="卡类型"
            >
            <template scope="scope">
                    <span>{{scope.row.card_type | cardTypeStatus}}</span>
            </template>
          </el-table-column> 
            <el-table-column
            prop="bind_user_phone"
            label="绑定用户手机号"
            >              
          </el-table-column>  
             <!--<el-table-column
            prop="data_status"
            label="是否启用"
            >    
            <template scope="scope">
              <span class="myColorRed" v-if="scope.row.data_status==0">失效</span>
              <span v-else>启用</span>
            </template>
          </el-table-column> -->
             <el-table-column
            prop="area_name"
            label="小区"
            >                     
          </el-table-column> 
            
           
          <el-table-column
            label="操作"
            width="150">
            <template scope="scope">
            	<el-button @click="getDetail(scope.row.card_id)" class="look" type="text" size="small">查看</el-button> |
            	<el-button @click="xiafa(scope.row.card_id)" class="look" type="text" size="small">发卡</el-button> |
             <el-button type="text" @click="modify(scope.row.card_id)" class="modifly" size="small">编辑</el-button>                         
            </template>
          </el-table-column>
        </el-table>
        <div class="block">                    
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page_num"
            :page-sizes="[10, 15]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
</style>

<script>
  import { getCardList , lssuedCard} from '@/api/entrance_guard';
  import { getRegion } from '@/utils/localstorage';
  import { extractAreaIds } from '@/utils/misc';
  
  export default {
    data(){
      return{  
        title:"门禁卡管理",    
        list:[],  

        page_size: null, //每页显示多少条
        page_num: null, //当前页码
        total:null, //总条数  
        
         // 小区选择
        regionOptions: [],
        regionOptSelected: [],
      
        id:null,
        index:null,
        idArr:[],
        area_id: -1,
        pages:1000,
        loading:false,
        multipleSelection: [],

        kw:"",
        deleteStatus:"",
        
      }
    },
    
   created() {
        document.title=this.title;
      	this.regionOptions = getRegion();
        this.regionOptSelected.push(this.regionOptions[0].value,this.regionOptions[0].children[0].value,this.regionOptions[0].children[0].children[0].value);
      
        this.fetchList();
    },
    
    computed: {
      areaIds() {
        return extractAreaIds(this.regionOptSelected, this.regionOptions);
      }
    },

    filters: {
       cardTypeStatus: function(value) {
        let src = '';
        switch (value) {
          case 1:
            src = '卡片';
          break;
          case 2:
            src = '身份证';
          break;
          case 3:
            src = '银行卡';
          break;
        }
        return src;    
      }           
  },
 

    methods:{
       fetchList() {
        let vm = this;
        const params = {
          page_size: this.page_size,
          area_id:vm.areaIds,
          kw: this.kw,
          page_num: this.page_num
        }
        vm.loading = true;
        getCardList(params)
        .then(response => {
          let res = response.data;
          vm.list = res.rows;
          vm.total = res.total;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;
          
          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        });
      },
      
      fuzzySearch() {
         this.fetchList();
      },
      
      handleSizeChange(val) {
        this.page_size = val;
        this.fetchList();
      },
      
      handleCurrentChange(val) {
        let pageNum=window.localStorage.getItem("pageNum");
        if(pageNum){
          this.page_num = Number(pageNum);
          window.localStorage.removeItem("pageNum");
        }else{
          this.page_num = val;
        }
        this.fetchList();
      },
      
      add:function(){
         this.$router.push({
           path:'/cardManageComonents',
           query: {type: 'add'}
         })
      },
      
      modify:function(card_no){
       this.$router.push({
         path:'/cardManageComonents',
         query: {type: 'update', id: card_no, pageNum: this.page_num}
       })
     },
     
      xiafa:function(card_id){
      	 let vm=this; 
      	 
         lssuedCard(card_id).then(response => {
           console.log("下发数据成功");
           vm.$message.success("下发数据成功！");
        }).catch(error => {
        	vm.$message.error("下发数据失败！");
          console.log("下发数据失败",error);
        })
      
     },
     
     getDetail:function(card_no){
        this.$router.push({
          path:'/cardManageComonents',
          query: {type: 'info', id: card_no}
        })
      },

     //----页码---
      handleSelectionChange(val) {
        this.multipleSelection = val;
        if(val.length!=0){
          for(var i = 0;i<val.length;i++){
            this.idArr.push(val[i].area_id)
          }
        }else{
          this.idArr = [];
        }
              
      },
       indexMethod(index) {
          return (this.page_num-1)*this.page_size + index + 1
      }
     
    }

  }


</script>



